package com.core.gwt.place.client;

import com.google.gwt.event.shared.EventBus;

/**
 * An abstract {@link Presenter} implementation that manages sub-presenters and
 * can enter {@link Mode}s.
 * 

 * 
 * @param <V>
 *            the view type
 */
public abstract class CompositePresenter<V> implements Presenter<V> {
	private final SubActivityManager subActivityManager;

	protected CompositePresenter() {
		subActivityManager = new SubActivityManager();
	}

	@Override
	public void detach() {
		subActivityManager.onStop();
	}

	@Override
	public String mayDetach() {
		String warning = null;
		if (warning == null) {
			warning = subActivityManager.mayStop();
		}
		return warning;
	}

	/**
	 * Attaches a {@link Presenter} to its widget and adds it to the list of
	 * managed sub-presenters.
	 * 
	 * @param <U>
	 *            the view type
	 * @param presenter
	 *            the presenter
	 * @param view
	 *            the widget
	 * @param eventBus
	 *            the event bus
	 */
	protected final <U> void attachPresenter(final Presenter<U> presenter,
			final U view, final EventBus eventBus) {
		subActivityManager.attach(presenter, view, eventBus);
	}
}
